package com.you.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.you.common.QueryPageParam;
import com.you.common.Result;
import com.you.entity.Goodstype;
import com.you.entity.Menu;
import com.you.entity.Storage;
import com.you.service.impl.GoodstypeServiceImpl;
import com.you.service.impl.StorageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 友友
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/goodstype")
public class GoodstypeController {

    @Autowired
    GoodstypeServiceImpl goodstypeService;

    @GetMapping("/list")
    public Result list(){
        List<Goodstype> list = goodstypeService.list();
        return Result.suc(list);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Goodstype goodstype) {
        return goodstypeService.save(goodstype)? Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public Result update(@RequestBody Goodstype goodstype) {
        return goodstypeService.updateById(goodstype)? Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id) {
        return goodstypeService.removeById(id)? Result.suc():Result.fail();
    }

    //自定义分页(查询)
    @PostMapping("/diyListPage")
    public Result diyListPage(@RequestBody QueryPageParam pageParam) {

        HashMap param = pageParam.getParam();
        String name = (String) param.get("name");

        Page<Goodstype> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Goodstype> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)){
            wrapper.like(Goodstype::getName, name);
        }

        IPage result = goodstypeService.pageDiy(page, wrapper);

        return Result.suc(result.getRecords(),result.getTotal());

    }
}


